#include "head.h"

//执行sql查询，返回是否成功
int exerect_cd_query(MYSQL *conn, request_t *request, response_t* response){

    MYSQL_RES *result;
    MYSQL_ROW row;
    //查询当前目录的上级目录
    if(strcmp(request->args,"..")==0){
        //存放查询语句
        if(strcmp(request->path, "/") == 0){
            strcpy(response->args, "/");
            return 0;
        }
        //printf("发现是..\n");
        char query[2048];

        //拼接绝对路径
        char path[2048]={0};
        strcpy(path,request->path);
        //printf("这边带着这个path去表里查: %s\n",path);

        snprintf(query,sizeof(query),"select path from files \
        where id = (select father_id from files \
        where path = '%s' and user_id = %d)",path, get_user_id(request->user_name, conn));
        int ret = mysql_query(conn,query);
        if(ret != 0){
            mysql_free_result(result);
            return 1;
        }        
        result = mysql_store_result(conn);
        row = mysql_fetch_row(result);
        //char buf[60] = {0};

        //strncpy(buf, row[0], sizeof(buf));
        
        // printf("buf: %s\n", row[0]);
        LOG(INFO, request->user_name, "cd return path", row[0]);
        strcpy(response->args, row[0]);

        mysql_free_result(result);
        return 0;
    }
    // if(request->args == NULL || request->args[0] == '\0'){
        
    //     return 0;
    // }
    //printf("发现是正常的参数\n");

    //查询指定路径名
    char query[2048];

    //拼接绝对路径
    char path[2048]={0};
    if(strcmp(request->path, "/") == 0){
        strcat(path,request->path);
        strcat(path,request->args);
    }else{
        strcat(path,request->path);
        strcat(path,"/");
        strcat(path,request->args);
    }

    //printf("这边带着这个path去表里查: %s\n",path);

    snprintf(query,sizeof(query),"select path from files \
    where path = '%s' and user_id = %d",\
    path, get_user_id(request->user_name, conn));

    int ret = mysql_query(conn,query);
    if(ret != 0){
        mysql_free_result(result);
        // printf("未查到\n");
        LOG(ERROR, request->user_name, "cd", "未查到");
        return 1;
    }
    result = mysql_store_result(conn);

    row = mysql_fetch_row(result);
    if(!row){
        // printf("row未查到\n");
        LOG(ERROR, request->user_name, "cd", "row未查到");
        return 1;
    }
    char buf[60] = {0};

    strncpy(buf, row[0], sizeof(buf));
    
    // printf("buf: %s\n", buf);
    LOG(INFO, request->user_name, "cd return path", buf);
    strcpy(response->args, buf);
    mysql_free_result(result);
    return 0;


}



int net_cd(request_t *request, int net_fd, MYSQL *mysql) {

    response_t response;
    memset(&response, 0, sizeof(response));


    int ret = exerect_cd_query(mysql, request, &response);
    response.status = ret;


    //response.status = 0;
    //strcpy(response.args, request->args);
    //printf("resonse_args: %s\n",response.args);
    send(net_fd, &response, sizeof(response), 0);
    // printf("cd: 响应了cd请求\n");
    LOG(INFO, request->user_name, "cd", "响应了cd请求");

    return 0;

}